package com.ruoyi.zjext.util;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.zjext.beans.MenuInfo;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author xi.yang
 * @create 2021-03-30 10:09
 **/
public class MenuUtil {
    private static List<MenuInfo> menuInfo;

    public static List<MenuInfo> allMenuInfo() {
        if (null == menuInfo) {
            menuInfo = init();
        }
        return menuInfo;
    }

    private static List<MenuInfo> init() {
        // 目前只支持两级菜单，后面再来优化
        List<MenuInfo> list = new ArrayList<>();
        try {
            ClassPathResource classPathResource = new ClassPathResource("menu.mu");
            byte[]  keywordsData = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
            String[] strs = new String(keywordsData, StandardCharsets.UTF_8).replace("\r\n", "\n")
                    .replace("\r", "\n")
                    .split("\n");
            List<String> lines = Arrays.asList(strs);
            // level,菜单
            Map<Integer, MenuInfo> levelMenu = new HashMap<>();
            int i = 0;
            for (String line : lines) {
                // 注释行不要
                if (line.startsWith(DESC_LINE_TAG)) {
                    continue;
                }
                if (line.startsWith(MENU_LINE_TAG)) {
                    MenuInfo cur = getMenuInfo(line);
                    if (cur.getLevel() == 1) {
                        cur.setId(Md5Utils.hash(cur.getTag()));
                        levelMenu.clear();
                        levelMenu.put(1, cur);
                        list.add(cur);
                        continue;
                    }
                    cur.setId(Md5Utils.hash(levelMenu.get(1).getTag() + "_" + cur.getTag()));
                    levelMenu.get(1).getChildren().add(cur);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    private static final String SPLIT_REGEX_T = "\t";
    private static final String DESC_LINE_TAG = "#";
    private static final String MENU_LINE_TAG = "-";
    /**
     * 解析菜单对象
     * @param line
     * @return
     */
    private static MenuInfo getMenuInfo(String line) {
        String[] tags = line.split(SPLIT_REGEX_T);
//        ## 目录级别 目录名 路径标识 图标 增删改查(搜索)1248
        return MenuInfo.builder()
                .level(tags[0].length())
                .title(tags[1])
                .tag(tags[2])
                .icon(tags[3])
                .func(Integer.parseInt(tags[4]))
                .build();
    }

    public static Set<String> getRoleMenu(List<SysRole> roles) {
        Set<String> result = new HashSet<>();
        if (null == roles) {
            return result;
        }
        for (SysRole role : roles) {
            if (null == role) {
                continue;
            }
            String[] tmp = StringUtils.split(role.getMenuKeys(), ",");
            if (null == tmp || tmp.length == 0) {
                continue;
            }
            result.addAll(Arrays.asList(tmp));
        }
        return result;
    }
}
